from OpenGL.GL import *

import LSystem2
from math import *
from random import *
from polygon import *

class LSystemGL(polyGL):
	def __init__(self,var1=1.2,var2=0):
		polyGL.__init__(self)
		self.r = [uniform(-1,1) for x in range(10)]

		self.L = LSystem2.LSystem()
		self.L.production = (0, 2, 0, 0), (0, 0, 2, 0), (0, 0, 0, 2)
		self.L.state = (0, var1, -var1/var2, var2), (0, -var1, var2, 0)
		self.L.zero = (0, -0.6, 0.6, 0.6)
		self.L.depth =  randint(2,3)
		
		self.append(self.L)
	
		
	def randomize(self):
	
		self.r = [uniform(-1,1) for x in range(10)]
	
		self.L = LSystem2.LSystem()
		self.L.production = (0, 2, 0, 0), (0, 0, 2, 0), (0, 0, 0, 2)
		self.L.state = (self.r[0], self.r[1], self.r[2], self.r[3]), (self.r[4], self.r[5], self.r[6], self.r[7])
	#	self.L.zero = (0, -0.6, 0.6, 0.6)
		self.L.zero = (self.r[8], self.r[9], self.r[4], self.r[2])

		self.L.depth = randint(2,2)

		
	def update(self, var):
		self.var = var
		self.L.production = (self.r[0]*sin(var*1),self.r[1]*sin(var*2),self.r[2]*sin(var*3),self.r[3]*sin(var*4)),(var,0,1,var/2+1),(0,0,0,-var+1)
		self.append(self.L)
		
	def compute(self):
		self.append(self.L)
		self.vert = [vector([q[0],q[1],q[2]]) for q in self.L]
		self.prepare()
		self.redraw = True
		
	def append(self, L):
	
		self.l = glGenLists(1)
		glNewList(self.l, GL_COMPILE)
		glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT)
		glDisable(GL_TEXTURE_2D)
		glDisable(GL_LIGHTING)
		glDisable(GL_DEPTH_TEST)
		glEnable(GL_BLEND)
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA)
		glEnable(GL_LINE_SMOOTH)
		glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
		glEnable(GL_POINT_SMOOTH)
		glHint(GL_POINT_SMOOTH_HINT, GL_NICEST)
		
		glLineWidth(5)
		glPointSize(1)
		
	#	glEnable (GL_LINE_STIPPLE);
	#	glLineStipple (1, 4);   #  dotted
	#	glLineStipple (1, $00FF);   #  dashed
	#	glLineStipple (1, $1C47);   #  dash/dot/dash
 		glEnable(GL_MAP1_VERTEX_3)

		Lc = [(q[0],q[1],q[2],q[3]) for q in L]

		for i in range(0,len(Lc),3):
			
			self.pts = [(a[1],a[2],a[3]) for a in Lc[i:i+4]]
			self.pts2 = [(a[1],a[0],a[3]) for a in Lc[i:i+4]]

			glMap1f(GL_MAP1_VERTEX_3, 0., 1.,self.pts)			glMapGrid1f(10, 0., 1.)
			glColor4f(1.0,0.0,0.0,1.0)			glEvalMesh1(GL_LINE, 0, 10)
			glColor4f(0.7,0.4,1.0,0.6)			glEvalMesh1(GL_LINE, 10, 13)
			glColor4f(.0,.0,.0,0.7)
			glEvalMesh1(GL_POINT, 0, -5 + int(abs(200*self.L.production[2][1])))
			
			glMap1f(GL_MAP1_VERTEX_3, 0., 1.,self.pts2)			glMapGrid1f(10, 0., 1.)
			glColor4f(1.0,1.0,1.0,0.7)			glEvalMesh1(GL_LINE, 0, 10)
			glColor4f(0.,0.,.0,0.2)			glEvalMesh1(GL_LINE, 10, 13)
			glColor4f(1.0,.0,.0,0.7)
			glEvalMesh1(GL_POINT, 0, -10 + int(abs(200*self.L.production[2][1])))			
			

	#	glBegin(GL_LINE_STRIP)
	#	for q in L:
	#		glVertex3d(q[1], q[2], q[3])
	#	glEnd()
		glPopAttrib()
		glEndList()
		
#	def draw(self):	
#		glCallList(self.l)